export default {
  computed: {
    fieldList() {
      return this.formConfig.fieldList || [];
    },
    formBtns() {
      const {
        formConfig: { btns },
      } = this;

      return btns.map(item => {
        const defaultConfig = {
          submit: {
            icon: 'el-icon-search',
          },
          reset: {
            icon: 'el-icon-refresh',
          },
        };
        return {
          type: 'primary',
          ...defaultConfig[item.key],
          ...item,
        };
      });
    },
    transformModel() {
      return this.formConfig.transformModel;
    },

    formProps() {
      return this.formConfig.props || { inline: true, size: 'small' };
    },

    beforeSubmit() {
      return this.formConfig.beforeSubmit;
    },
  },

  methods: {
    handleFormChange(val, fieldConfig) {
      if (fieldConfig.isChangeToSubmit) {
        this.handleFormBtnClick('submit');
      }
    },
    handleFormBtnClick(key) {
      const params = this.$refs.form.getTransformModel();
      // 默认按钮的处理
      const handlers = {
        submit: () => {
          this.$refs.form.submit().then(searchParams => {
            if (!this.beforeSubmit || (this.beforeSubmit && this.beforeSubmit(searchParams))) {
              this.searchTableData(searchParams);
            }
          });
        },
        reset: () => {
          this.$refs.form.reset().then(searchParams => {
            this.searchTableData(searchParams);
          });
        },

        batchDelete: () => {
          this.handleBatchDelete();
        },
      };

      if (handlers[key]) {
        handlers[key]();
      }
      this.$emit('handleFormBtnClick', key, params);
    },
  },
};
